# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

load(
    "//:build_defs.bzl",
    "cc_binary_mozc",
    "cc_library_mozc",
    "cc_test_mozc",
    "macos_application_mozc",
    "objc_library_mozc",
    "select_mozc",
)
load(
    "//:config.bzl",
    "BRANDING",
    "MACOS_BUNDLE_ID_PREFIX",
)

package(default_visibility = ["//:__subpackages__"])

cc_library_mozc(
    name = "renderer_client",
    srcs = ["renderer_client.cc"],
    hdrs = ["renderer_client.h"],
    deps = [
        ":renderer_interface",
        "//base",
        "//base:clock",
        "//base:logging",
        "//base:port",
        "//base:process",
        "//base:run_level",
        "//base:system_util",
        "//base:thread",
        "//base:util",
        "//base:version",
        "//ipc",
        "//ipc:named_event",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/synchronization",
    ] + select_mozc(
        ios = ["//base:mac_util"],
    ),
)

cc_test_mozc(
    name = "renderer_client_test",
    size = "medium",
    srcs = ["renderer_client_test.cc"],
    requires_full_emulation = False,
    tags = ["no_android"],
    deps = [
        ":renderer_client",
        ":renderer_interface",
        "//base",
        "//base:logging",
        "//base:number_util",
        "//base:port",
        "//base:version",
        "//ipc",
        "//protocol:commands_cc_proto",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
    ],
)

cc_library_mozc(
    name = "renderer_server",
    srcs = ["renderer_server.cc"],
    hdrs = ["renderer_server.h"],
    deps = [
        ":renderer_interface",
        "//base",
        "//base:logging",
        "//base:port",
        "//base:system_util",
        "//client",
        "//client:client_interface",
        "//config:config_handler",
        "//ipc",
        "//ipc:named_event",
        "//ipc:process_watch_dog",
        "//protocol:config_cc_proto",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/memory",
    ],
)

cc_test_mozc(
    name = "renderer_server_test",
    size = "medium",
    srcs = ["renderer_server_test.cc"],
    requires_full_emulation = False,
    tags = ["no_android"],
    deps = [
        ":renderer_client",
        ":renderer_interface",
        ":renderer_server",
        "//base",
        "//base:logging",
        "//base:port",
        "//base:system_util",
        "//base:util",
        "//ipc:ipc_test_util",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
        "@com_google_absl//absl/flags:flag",
    ],
)

cc_library_mozc(
    name = "table_layout",
    srcs = [
        "table_layout.cc",
        "table_layout_interface.h",
    ],
    hdrs = ["table_layout.h"],
    deps = [
        "//base",
        "//base:coordinates",
        "//base:logging",
        "//base:port",
    ],
)

cc_test_mozc(
    name = "table_layout_test",
    size = "small",
    srcs = [
        "table_layout_interface.h",
        "table_layout_test.cc",
    ],
    requires_full_emulation = False,
    tags = ["no_android"],
    deps = [
        ":table_layout",
        "//base",
        "//testing:gunit_main",
    ],
)

cc_library_mozc(
    name = "window_util",
    srcs = ["window_util.cc"],
    hdrs = ["window_util.h"],
    deps = [
        "//base",
        "//base:coordinates",
    ],
)

cc_library_mozc(
    name = "renderer_interface",
    hdrs = ["renderer_interface.h"],
)

cc_library_mozc(
    name = "unix_const",
    hdrs = ["unix/const.h"],
    deps = ["//base:port"],
)

cc_library_mozc(
    name = "renderer_mock",
    testonly = 1,
    hdrs = ["renderer_mock.h"],
    deps = [
        ":renderer_interface",
        "//testing:gunit",
    ],
)

cc_test_mozc(
    name = "window_util_test",
    size = "small",
    srcs = ["window_util_test.cc"],
    requires_full_emulation = False,
    tags = ["no_android"],
    deps = [
        ":window_util",
        "//base",
        "//base:coordinates",
        "//testing:gunit_main",
    ],
)

cc_library_mozc(
    name = "renderer_style_handler",
    srcs = ["renderer_style_handler.cc"],
    hdrs = ["renderer_style_handler.h"],
    deps = [
        "//base:singleton",
        "//protocol:renderer_cc_proto",
    ],
)

cc_test_mozc(
    name = "renderer_style_handler_test",
    size = "small",
    srcs = ["renderer_style_handler_test.cc"],
    requires_full_emulation = False,
    deps = [
        ":renderer_style_handler",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
    ],
)

# This rule is only for Linux application build.
cc_library_mozc(
    name = "gtk_mozc_renderer_lib",
    srcs = select_mozc(
        linux = [
            "unix/cairo_factory.cc",
            "unix/cairo_wrapper.cc",
            "unix/candidate_window.cc",
            "unix/draw_tool.cc",
            "unix/font_spec.cc",
            "unix/gtk_window_base.cc",
            "unix/gtk_wrapper.cc",
            "unix/infolist_window.cc",
            "unix/pango_wrapper.cc",
            "unix/text_renderer.cc",
            "unix/unix_renderer.cc",
            "unix/unix_server.cc",
            "unix/window_manager.cc",
        ],
    ),
    hdrs = select_mozc(
        linux = [
            "unix/cairo_factory.h",
            "unix/cairo_factory_interface.h",
            "unix/cairo_wrapper.h",
            "unix/cairo_wrapper_interface.h",
            "unix/candidate_window.h",
            "unix/const.h",
            "unix/draw_tool.h",
            "unix/draw_tool_interface.h",
            "unix/font_spec.h",
            "unix/font_spec_interface.h",
            "unix/gtk_window_base.h",
            "unix/gtk_window_interface.h",
            "unix/gtk_wrapper.h",
            "unix/gtk_wrapper_interface.h",
            "unix/infolist_window.h",
            "unix/pango_wrapper.h",
            "unix/pango_wrapper_interface.h",
            "unix/text_renderer.h",
            "unix/text_renderer_interface.h",
            "unix/unix_renderer.h",
            "unix/unix_server.h",
            "unix/window_manager.h",
            "unix/window_manager_interface.h",
        ],
    ),
    deps = [
        "//base",
        "//client",
        "//config:stats_config_util",
        "//ipc",
        "//protocol:config_cc_proto",
        "//protocol:renderer_cc_proto",
        "@com_google_absl//absl/synchronization",
        ":renderer_server",
        ":renderer_style_handler",
        ":table_layout",
        ":window_util",
    ] + select_mozc(
        oss_linux = ["@gtk2//:gtk2"],
    ),
)

# This rule is only for Linux application build.
cc_binary_mozc(
    name = "mozc_renderer",
    srcs = ["mozc_renderer_main.cc"],
    defines = select_mozc(
        default = ["ENABLE_QT_RENDERER"],
        linux = ["ENABLE_QT_RENDERER"],
    ),
    deps = [
        ":table_layout",
        "//base:crash_report_handler",
        "//base:init_mozc",
        "//base:run_level",
        "//base:system_util",
        "//base:util",
        "//config:stats_config_util",
        "@com_google_absl//absl/flags:flag",
    ] + select_mozc(
        default = [
            "//renderer/qt:qt_renderer_lib",
            "//renderer/qt:qt_server",
            "//renderer/qt:qt_window_manager",
        ],
        linux = [
            "//renderer/qt:qt_renderer_lib",
            "//renderer/qt:qt_server",
            "//renderer/qt:qt_window_manager",
        ],
        windows = [
            "//base:win_util",
            "//base:winmain",
        ],
    ),
)

cc_binary_mozc(
    name = "gtk_mozc_renderer",
    srcs = ["mozc_renderer_main.cc"],
    defines = select_mozc(linux = ["ENABLE_GTK_RENDERER"]),
    deps = [
        ":gtk_mozc_renderer_lib",
        ":renderer_client",
        ":table_layout",
        "//base:crash_report_handler",
        "//base:init_mozc",
        "//base:run_level",
        "//base:system_util",
        "//base:util",
        "//config:stats_config_util",
        "@com_google_absl//absl/flags:flag",
    ],
)

cc_binary_mozc(
    name = "qt_mozc_renderer",
    srcs = ["mozc_renderer_main.cc"],
    defines = ["ENABLE_QT_RENDERER"],
    deps = [
        ":renderer_server",
        ":table_layout",
        "//base:crash_report_handler",
        "//base:init_mozc",
        "//base:run_level",
        "//base:system_util",
        "//base:util",
        "//config:stats_config_util",
        "//renderer/qt:qt_renderer_lib",
        "//renderer/qt:qt_server",
        "//renderer/qt:qt_window_manager",
        "@com_google_absl//absl/flags:flag",
    ],
)

# This rule is only for Linux application build.
cc_test_mozc(
    name = "gtk_renderer_test",
    srcs = select_mozc(
        linux = [
            "renderer_mock.h",
            "table_layout_mock.h",
            "unix/cairo_factory_interface.h",
            "unix/cairo_factory_mock.h",
            "unix/cairo_wrapper_interface.h",
            "unix/cairo_wrapper_mock.h",
            "unix/candidate_window_test.cc",
            "unix/draw_tool_interface.h",
            "unix/draw_tool_mock.h",
            "unix/draw_tool_test.cc",
            "unix/font_spec_interface.h",
            "unix/font_spec_test.cc",
            "unix/gtk_window_base_test.cc",
            "unix/gtk_window_interface.h",
            "unix/gtk_window_mock.h",
            "unix/gtk_wrapper_interface.h",
            "unix/gtk_wrapper_mock.h",
            "unix/infolist_window_test.cc",
            "unix/pango_wrapper_interface.h",
            "unix/text_renderer_interface.h",
            "unix/text_renderer_mock.h",
            "unix/text_renderer_test.cc",
            "unix/unix_renderer_test.cc",
            "unix/unix_server_test.cc",
            "unix/window_manager_interface.h",
            "unix/window_manager_mock.h",
            "unix/window_manager_test.cc",
        ],
    ),
    deps = [
        ":gtk_mozc_renderer_lib",
        ":renderer_interface",
        ":renderer_style_handler",
        "//base:coordinates",
        "//base:logging",
        "//base:port",
        "//base:system_util",
        "//base:util",
        "//client:client_interface",
        "//protocol:renderer_cc_proto",
        "//testing:gunit_main",
        "@com_google_absl//absl/flags:flag",
    ] + select_mozc(
        oss_linux = ["@gtk2//:gtk2"],
    ),
)

# macOS
macos_application_mozc(
    name = "mozc_renderer_macos",
    app_icons = ["//data/images/mac:product_icon.icns"],
    bundle_id = MACOS_BUNDLE_ID_PREFIX + ".Renderer",
    bundle_name = BRANDING + "Renderer",
    infoplists = ["mac/Info.plist"],
    resources = ["//data/images/mac:candidate_window_logo.tiff"],
    deps = [":mozc_renderer_main_macos"],
    # When we support Breakpad, uncomment the following block.
    # additional_contents = {
    #     "[Breakpad]" : "Frameworks",
    # },
)

cc_library_mozc(
    name = "mozc_renderer_main_macos",
    srcs = select_mozc(macos = ["mozc_renderer_main.cc"]),
    deps = [
        ":renderer_client",
        ":table_layout",
        "//base:crash_report_handler",
        "//base:init_mozc",
        "//base:run_level",
        "//base:system_util",
        "//base:util",
        "//config:stats_config_util",
        "@com_google_absl//absl/flags:flag",
    ] + select_mozc(
        macos = [":mozc_renderer_lib_macos"],
        windows = [
            "//base:win_util",
            "//base:winmain",
        ],
    ),
)

# Because proto_library cannot be in deps of objc_library,
# cc_library as a wrapper is necessary as a workaround.
cc_library_mozc(
    name = "proto_wrapper",
    deps = [
        "//protocol:commands_cc_proto",
        "//protocol:config_cc_proto",
        "//protocol:renderer_cc_proto",
    ],
)

objc_library_mozc(
    name = "mozc_renderer_lib_macos",
    srcs = select_mozc(macos = [
        "mac/mac_server.mm",
        "mac/mac_server_send_command.mm",
        "mac/CandidateController.mm",
        "mac/CandidateWindow.mm",
        "mac/CandidateView.mm",
        "mac/InfolistWindow.mm",
        "mac/InfolistView.mm",
        "mac/RendererBaseWindow.mm",
        "mac/mac_view_util.mm",
    ]),
    hdrs = select_mozc(macos = [
        "mac/mac_server.h",
        "mac/mac_server_send_command.h",
        "mac/CandidateController.h",
        "mac/CandidateWindow.h",
        "mac/CandidateView.h",
        "mac/InfolistWindow.h",
        "mac/InfolistView.h",
        "mac/RendererBaseWindow.h",
        "mac/mac_view_util.h",
    ]),
    sdk_frameworks = [
        "Carbon",
        "Foundation",
    ],
    deps = select_mozc(macos = [
        "//base:base",
        "//base:crash_report_handler",
        "//client:client",
        "//config:stats_config_util",
        "//ipc:ipc",
        "//mac:common",
        "@com_google_absl//absl/base",
        ":proto_wrapper",
        ":renderer_server",
        ":renderer_style_handler",
        ":table_layout",
        ":window_util",
    ]),
)
